﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Input;
using System.Text.RegularExpressions;
using System.Windows.Controls;
using System.Windows.Media;


namespace CommomFunctions
{
    public partial class NumElementosDialogBox : Window
    {
        int numMax;
        Grid gd = new Grid();
        Label lblNumElementos = new Label();
        public TextBox txtNumElementos = new TextBox();
        Button btnOk = new Button();
        Button btnCancel = new Button();

        public NumElementosDialogBox(int numMax)
        {
            this.numMax = numMax;
            this.Title = "Número de Elementos";
            this.Height = 200;
            this.Width = 280;
            this.MinHeight = 10;
            this.MinWidth = 280;
            this.ResizeMode = ResizeMode.CanResizeWithGrip;
            this.ShowInTaskbar = false;
            this.WindowStartupLocation = WindowStartupLocation.CenterOwner;

            this.AddChild(gd);

            defineGrid();

            adicionaAoGrid();

            inicializaComponentes();

        }

        private void defineGrid()
        {
            ColumnDefinition colDef1 = new ColumnDefinition();
            gd.ColumnDefinitions.Add(colDef1);

            RowDefinition rowDef1 = new RowDefinition();
            RowDefinition rowDef2 = new RowDefinition();
            RowDefinition rowDef3 = new RowDefinition();
            gd.RowDefinitions.Add(rowDef1);
            gd.RowDefinitions.Add(rowDef2);
            gd.RowDefinitions.Add(rowDef3);

            colDef1.Width = new GridLength(258);

            rowDef1.Height = GridLength.Auto;
            rowDef1.MinHeight = 29;

            rowDef2.Height = GridLength.Auto;
            rowDef2.MinHeight = 29;

            rowDef3.Height = GridLength.Auto;
            rowDef3.MinHeight = 29;


        }

        private void adicionaAoGrid()
        {
            gd.Children.Add(lblNumElementos);
            Grid.SetRow(lblNumElementos, 0);

            gd.Children.Add(txtNumElementos);
            Grid.SetRow(txtNumElementos, 1);
            Grid.SetRowSpan(txtNumElementos, 1);

            gd.Children.Add(btnOk);
            Grid.SetRow(btnOk, 3);

            gd.Children.Add(btnCancel);
            Grid.SetRow(btnCancel, 3);
        }

        private void inicializaComponentes()
        {

            lblNumElementos.Content = "O número máximo de elementos é " + numMax.ToString();
            lblNumElementos.Margin = new Thickness(0, 3, 0, 5);
            lblNumElementos.FontSize = 14;
            lblNumElementos.Foreground = Brushes.DarkCyan;

            txtNumElementos.Margin = new Thickness(95, 15, 93, 15);

            //aprender a usar style sem ser no xaml
            //Style st = new Style();
            //st.TargetType = typeof(Button);
            //st.Setters.Add(new Setter(Button.WidthProperty,70));
            //st.Setters.Add(new Setter(Button.HeightProperty,25));
            //st.Setters.Add(new Setter(Button.MarginProperty,new Thickness(5,0,50,0)));


            //  btnOk.ApplyStyle(st) = st;

            btnOk.Width = 70;
            btnOk.Height = 25;
            btnOk.Margin = new Thickness(32, 9, 0, 16);
            btnOk.Click += okButton_Click;
            btnOk.HorizontalAlignment = HorizontalAlignment.Left;
            btnOk.Content = "Ok";

            btnCancel.Width = 70;
            btnCancel.Height = 25;
            btnCancel.Margin = new Thickness(0, 11, 22, 21);
            btnCancel.Click += cancelButton_Click;
            btnCancel.HorizontalAlignment = HorizontalAlignment.Right;
            btnCancel.Content = "Cancelar";

        }


        void cancelButton_Click(object sender, RoutedEventArgs e)
        {
            // Dialog box canceled
            this.DialogResult = false;
        }

        void okButton_Click(object sender, RoutedEventArgs e)
        {
            // Don't accept the dialog box if there is invalid data
            if (!isValid()) return;

            this.DialogResult = true;

        }

        bool isValid()
        {
            Regex rx = new Regex("([0-9])+");

            if (!rx.IsMatch(txtNumElementos.Text.ToString()))
                return false;

            float ne = float.Parse(txtNumElementos.Text.ToString());
            int neInt = (int)ne;

            if (neInt < 1)
            {
                lblNumElementos.Content = "O número mínimo de elementos é 1";


            }
            else if (neInt > numMax)
            {
                lblNumElementos.Content = "O número máximo de elementos é " + numMax.ToString();

            }
            else return true;

            lblNumElementos.Foreground = Brushes.Red;
            return false;


        }


    }
}
